﻿#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

extern int my_strcmp(char s1[], char s2[]);
extern int my_strcmp_myself(char s1[], char s2[]);
extern int twoStrcmp(char s1[], char s2[]);

//main63：自定义strcmp字符串比较函数
int main63()
{
	char s1[] = "hello world";
	char s2[] = "hello abc";

	int flag = my_strcmp(s1, s2);
	if (flag == 0)
	{
		printf("%s 等于 %s\n", s1, s2);
	}
	else if (flag > 0)
	{
		printf("%s 大于 %s\n", s1, s2);//hello world 大于 hello abc
	}
	else
	{
		printf("%s 小于 %s\n", s1, s2);
	}

	system("pause");
	return EXIT_SUCCESS;
}
int my_strcmp(char s1[], char s2[])
{
	int i = 0;
	while (s1[i] != '\0' && s2[i] != '\0')
	{
		if (s1[i] != s2[i])
		{
			return s1[i] - s2[i];
		}
		i++;
	}
	//1.注意：判断另外一个字符串，是否已经为结束；
	if (s1[i] != '\0' || s2[i] != '\0')
	{
		return s1[i] - s2[i];
	}
	//2.两个字符串相等；
	return 0;
}

int my_strcmp_myself(char s1[], char s2[])
{
	int len1 = strlen(s1);
	int len2 = strlen(s2);
	if (len1 > len2)
	{
		return twoStrcmp(s1, s2);
	}
	else if (len1 < len2)
	{
		return -twoStrcmp(s2, s1);
	}
	else {
		return twoStrcmp(s1, s2);
	}
}

int twoStrcmp(char s1[], char s2[])
{
	int flag;
	int index = 0;
	while (s1[index] != '\0')
	{
		if (s1[index] == s2[index])
		{
			flag = 0;
		}
		else if (s1[index] > s2[index])
		{
			flag = 1;
			return flag;
		}
		else if (s1[index] < s2[index])
		{
			flag = -1;
			return flag;
		}
		//printf("%c\n", s1[index]);
		index++;
	}
	return flag;
}
